#include "home.h"
#include "ui_home.h"

home::home(Gobang_Network *gn, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::home)
{
    ui->setupUi(this);

    this->gn = gn;

    /* 绑定一些必要槽 */
    connect(this->ui->listWidget, SIGNAL(itemClicked(QListWidgetItem *)),
                this, SLOT(itemClicked(QListWidgetItem *)));

    /* 服务器给我列表 */
    connect(gn->t, SIGNAL(list_addItem(uint16_t, QString)),
                this, SLOT(list_addItem(uint16_t, QString)));

    for (int i=0; i < 40; i++) {
        room_list[i].fd = 0;
        room_list[i].fd_cpy = 0;
    }

    //设置边距、间距
    ui->listWidget->setContentsMargins(0, 0, 0, 0);
    ui->listWidget->setSpacing(4);

    ui->listWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    ui->listWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
}

home::~home()
{
    delete ui;
}

void home::setSize(int x, int y, int w, int h, QRect mypos)
{
    this->ui->verticalLayoutWidget->setGeometry(x,y,w,h);
    this->ui->listWidget->resize(w,h);
    this->list_item_setWidth(w);
    this->mypos = mypos;
}

void home::create_post()
{
    log("创建房间名称:%s",room_text.toUtf8().data());
    gn->t->tx(A_POST_CREATE_HOUSE,(uint8_t *)room_text.toUtf8().data(), (uint8_t)room_text.toUtf8().length()+1);
}

void home::create_fail()
{
    log("create_fail");

    QMessageBox::warning(this, "失败", "超时，请检查网络连接");
}

void home::itemClicked(QListWidgetItem *item)
{
    log("发生点击");
    qDebug()<<item_getName(item);
    this->join_room(item_getID(item));
}

uint16_t home::item_getID(QListWidgetItem *item)
{
    QWidget* pwig = ui->listWidget->itemWidget(item);
    QLabel * label = pwig->findChild<QLabel *>("label_id");
    return  label->text().toUInt();
}

QString home::item_getName(QListWidgetItem *item)
{
    QWidget* pwig = ui->listWidget->itemWidget(item);
    QLabel * label = pwig->findChild<QLabel *>("label_name");
    return  label->text();
}

void home::purge_list(void)
{
    for (int i=0; i<40; i++) {
        room_list[i].fd = 0;
    }

    ui->listWidget->clear();

    log("上面叫我清除列表");
}

void home::list_addItem(uint16_t fd, QString str)
{
    if (fd == 1) {
        purge_list();
        return;
    }

    if (fd == 0) {
        return;
    }

    /* 弄那个列表 */
    room_item *ri = new room_item();
    ri->setInfo(fd, str);

    QListWidgetItem * m_item = new QListWidgetItem(ui->listWidget);
    ui->listWidget->setItemWidget(m_item, ri);
}


void home::list_item_setWidth(int width)
{
    for (int j = 0; j < ui->listWidget->count(); j++) {
        QListWidgetItem *item = ui->listWidget->item(j);
        QWidget* pwig = ui->listWidget->itemWidget(item);
        QWidget * widget = pwig->findChild<QWidget *>("horizontalLayoutWidget");
        item->setSizeHint(QSize(width-10,100));
        widget->resize(width-20-10, 100);
    }
}

void home::join_room(uint16_t fd)
{
    log("申请加入");
    gn->t->tx_uint16(A_JOIN_ROOM_ID, fd);
}


void home::on_toolButton_update_clicked()
{

}

void home::on_toolButton_createRoom_clicked()
{
    log("申请棋盘");
    on_create_room();

    if (room_text == "") {
        return ;
    }

    lm = new LoadMessage("正在申请...", 2);

    int x = (this->mypos.x() + (this->mypos.width() / 2)) - (lm->width()/2);
    int y = (this->mypos.y() + (this->mypos.height()/ 2)) - (lm->height()/2);

    lm->move(x,y);

    connect(lm,&LoadMessage::create_fail, this,&home::create_fail);
    connect(lm,&LoadMessage::create_post, this,&home::create_post);
}
